package at.ac.tuwien.mg;

import java.io.IOException;
import java.net.URI;

import javax.ws.rs.core.UriBuilder;

import org.glassfish.grizzly.http.server.HttpServer;

import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;

public class StartServices {

	private static URI getBaseURI() {
		return UriBuilder.fromUri("http://localhost/").port(9998).build();
	}

	public static final URI BASE_URI = getBaseURI();

	protected static HttpServer startServer() throws IOException {
		System.out.println("Starting grizzly (for webservices...");
		ResourceConfig rc = new PackagesResourceConfig(
				"at.ac.tuwien.mg.webservice");
		return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HttpServer httpServer;
		try {
			httpServer = startServer();
			System.out.println(String.format("Jersey app started with WADL available at "
					+ "%sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...",
					BASE_URI, BASE_URI));
			System.in.read();
			httpServer.stop();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
